<template>
  <div class="back" style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #ccc">
    <div style="display: flex; background-color: rgb(236, 247, 237); width: 55%; border-radius: 5px; overflow: hidden">
      <div style="flex: 1">
        <img src="@/assets/register.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="registerRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">欢迎注册高校创新创业管理系统</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入用户名" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请确认密码" v-model="user.confirmPass"></el-input>
          </el-form-item>
          <el-form-item prop="role">
            <el-radio-group v-model="user.role">
              <el-radio label="学生"></el-radio>
              <el-radio label="教师"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;background-color: #6e77f2;" @click="register">注 册</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">已经有账号了？请 <span style="color: #6e77f2; cursor: pointer" @click="$router.push('/login')">登录</span></div>
          </div>
        </el-form>
      </div>
    </div>
    <!-- 时间显示组件 -->
    <div class="clock">
      <div class="time">{{ time }}</div>
      <div class="date">{{ date }}</div>
    </div>
  </div>
</template>

<script>

export default {
  name: "Register",
  data() {
    // 两次密码校验
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass!== this.user.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      user: {
        username: '',
        password: '',
        confirmPass: ''
      },
      time: "",
      date: "",
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPass: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'blur' }
        ],
      }
    }
  },
  created() {

  },
  mounted() {
    this.$nextTick(() => {
      setInterval(this.update_clock, 1000); 
    })
  },
  methods: {
    update_clock() {
      var d = new Date();
      var year = d.getFullYear();
      var mon = d.getMonth() + 1;
      var day = d.getDate();
      var hour = d.getHours();
      var minute = d.getMinutes();
      if (minute < 10) {
        minute = "0" + minute;
      }
      this.time = hour + ":" + minute;
      this.date = year + "/" + mon + "/" + day
    },
    register() {
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/register', this.user).then(res => {
            if (res.code === '200') {
              this.$router.push('/login')
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  @keyframes animationmy{
    0%{
      background-color: rgb(243, 237, 239);
    }
    25%{
      background-color: rgb(241, 201, 209);
    }
    50%{
      background-color: rgb(242, 147, 183);
    }
    75%{
      background-color: rgb(241, 201, 209);
    }
    100%{
      background-color: rgb(243, 237, 239);
    }
  }
.back{
    animation: animationmy 5s linear infinite;
    height: 100vh;
  }
 .clock{
    color: black;
    position: absolute;
    top: 75%;
    left: 10%;
    text-align: left;
  }
 .time{
    font-size: 70px;
  }
 .date{
    font-size: 35px;
  }
</style>